<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="outTradeNo">
        <el-input v-model="queryParams.outTradeNo" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="预付单号" prop="transactionId">
        <el-input v-model="queryParams.transactionId" placeholder="请输入预付单号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户id" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入用户id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable>
          <el-option v-for="dict in dict.type.order_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="支付方式" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择支付方式" clearable>
          <el-option v-for="dict in dict.type.pay_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="联系人名称" prop="contactName">
        <el-input v-model="queryParams.contactName" placeholder="请输入联系人名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactNumber">
        <el-input v-model="queryParams.contactNumber" placeholder="请输入联系电话" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="daterangeCreateTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime">
        <el-date-picker clearable v-model="queryParams.updateTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['dentalOrder:dentalOrder:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['dentalOrder:dentalOrder:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['dentalOrder:dentalOrder:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['dentalOrder:dentalOrder:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dentalOrderList" @selection-change="handleSelectionChange"
      ref="dentalOrderTable">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="outTradeNo" width="300px"/>
      <!-- <el-table-column label="预付单号" align="center" prop="transactionId" /> -->
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="订单状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center" prop="payType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pay_type" :value="scope.row.payType" />
        </template>
      </el-table-column>
      <el-table-column label="余额支付金额" align="center" prop="balancePayAmount"  width="100"/>
      <el-table-column label="积分支付金额" align="center" prop="scorePayAmount" width="100"/>
      <el-table-column label="总金额" align="center" prop="totalAmount" />
      <!-- <el-table-column label="联系人名称" align="center" prop="contactName" />
      <el-table-column label="联系电话" align="center" prop="contactNumber" /> -->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="用户备注" align="center" prop="userRemark" />
      <!-- <el-table-column label="固定类" align="center" prop="fixedClass" /> -->
      <!-- <el-table-column label="固定类" align="center" prop="fixedClass" width="300px">
        <template slot-scope="scope">
          <tooth-diagram :fixedClass="parseFixedClass(scope.row.fixedClass)" @tooth-click="handleToothClick" />
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="活动类" align="center" prop="activityClass" />
      <el-table-column label="排牙类" align="center" prop="paiClass" /> -->
      <!-- 操作列 -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-search" @click="toggleDetails(scope.row)">
            {{ scope.row.showDetails ? '关闭详情' : '查看详情' }}
          </el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['dentalOrder:dentalOrder:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['dentalOrder:dentalOrder:remove']">删除</el-button> -->
        </template>
      </el-table-column>

      <!-- 详情展开行 -->
      <el-table-column type="expand">
        <template slot-scope="scope">
          <div v-if="scope.row.showDetails" class="tooth-diagram-container">
            <h3>固定类</h3>
            <tooth-diagram :fixedClass="scope.row.fixedClass" :activityClass="[]" :paiClass="[]"
              @tooth-click="handleToothClick" />

            <h3>活动类</h3> <!-- 新增活动类展示 -->
            <tooth-diagram :fixedClass="[]" :activityClass="scope.row.activityClass" :paiClass="[]"
              @tooth-click="handleToothClick" />

            <h3>排牙类</h3> <!-- 新增拍片类展示 -->
            <tooth-diagram :fixedClass="[]" :activityClass="[]" :paiClass="scope.row.paiClass"
              @tooth-click="handleToothClick" />
          </div>
        </template>
      </el-table-column>



    </el-table>

    <!-- 牙齿模型展示部分 -->
    <!-- <div v-for="(row, index) in dentalOrderList" :key="index">
      <div v-if="row.showDetails" class="tooth-diagram-container">
        <h3>固定类</h3>
        <tooth-diagram :fixedClass="row.fixedClass" @tooth-click="handleToothClick" />
      </div>
    </div> -->

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改产品订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="预付单号" prop="transactionId">
          <el-input v-model="form.transactionId" placeholder="请输入预付单号" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="订单状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in dict.type.order_status" :key="dict.value" :label="dict.value">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="支付方式" prop="payType">
          <el-select v-model="form.payType" placeholder="请选择支付方式">
            <el-option v-for="dict in dict.type.pay_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="余额支付金额" prop="balancePayAmount">
          <el-input v-model="form.balancePayAmount" placeholder="请输入余额支付金额" />
        </el-form-item>
        <el-form-item label="积分支付金额" prop="scorePayAmount">
          <el-input v-model="form.scorePayAmount" placeholder="请输入积分支付金额" />
        </el-form-item>
        <el-form-item label="总金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入总金额" />
        </el-form-item>
        <el-form-item label="联系人名称" prop="contactName">
          <el-input v-model="form.contactName" placeholder="请输入联系人名称" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactNumber">
          <el-input v-model="form.contactNumber" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="用户备注" prop="userRemark">
          <el-input v-model="form.userRemark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="固定类" prop="fixedClass">
          <el-input v-model="form.fixedClass" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="活动类" prop="activityClass">
          <el-input v-model="form.activityClass" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="排牙类" prop="paiClass">
          <el-input v-model="form.paiClass" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDentalOrder, getDentalOrder, delDentalOrder, addDentalOrder, updateDentalOrder } from "@/api/dentalOrder/dentalOrder";

import ToothDiagram from '@/api/dentalOrder/components/ToothDiagram.vue';  // 假设组件在 components 文件夹中


export default {
  name: "DentalOrder",
  dicts: ['pay_type', 'order_status'],
  components: {
    ToothDiagram
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品订单表格数据
      dentalOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 排牙类时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        outTradeNo: null,
        transactionId: null,
        userId: null,
        status: null,
        payType: null,
        contactName: null,
        contactNumber: null,
        createTime: null,
        updateTime: null,
        userRemark: null,
        fixedClass: null,
        activityClass: null,
        paiClass: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "订单状态不能为空", trigger: "change" }
        ],
        contactName: [
          { required: true, message: "联系人名称不能为空", trigger: "blur" }
        ],
        contactNumber: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    toggleDetails(row) {
      // 切换显示详情的状态
      row.showDetails = !row.showDetails;

      // 手动展开或折叠表格行
      this.$refs.dentalOrderTable.toggleRowExpansion(row, row.showDetails);
    },
    parseFixedClass(fixedClass) {
      // 如果 fixedClass 是字符串类型，解析为对象数组
      return typeof fixedClass === 'string' ? JSON.parse(fixedClass) : fixedClass;
    },
    handleToothClick(tooth) {
      // this.$message({
      //   // message: `牙齿位置: ${tooth.toothPosition}, 序号: ${tooth.toothNo}, 数量: ${tooth.num}`,
      //   message: `产品名称: ${tooth.productName}, 数量: ${tooth.num}`,
      //   type: 'success'
      // });
      this.$alert(`产品名称: ${tooth.productName}<br> 材质: ${tooth.material}<br> 数量: ${tooth.num}`, '产品描述', {
        confirmButtonText: '确定',
        dangerouslyUseHTMLString: true, // 允许使用 HTML 字符串
        // callback: action => {
        //   this.$message({
        //     type: 'info',
        //     message: `action: ${action}`
        //   });
        // }
      });
    },
    /** 查询产品订单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listDentalOrder(this.queryParams).then(response => {
        this.dentalOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        outTradeNo: null,
        transactionId: null,
        userId: null,
        status: null,
        payType: null,
        balancePayAmount: null,
        scorePayAmount: null,
        totalAmount: null,
        contactName: null,
        contactNumber: null,
        createTime: null,
        updateTime: null,
        remark: null,
        userRemark: null,
        fixedClass: null,
        activityClass: null,
        paiClass: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.outTradeNo)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const outTradeNo = row.outTradeNo || this.ids
      getDentalOrder(outTradeNo).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.outTradeNo != null) {
            updateDentalOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDentalOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const outTradeNos = row.outTradeNo || this.ids;
      this.$modal.confirm('是否确认删除产品订单编号为"' + outTradeNos + '"的数据项？').then(function () {
        return delDentalOrder(outTradeNos);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dentalOrder/dentalOrder/export', {
        ...this.queryParams
      }, `dentalOrder_${new Date().getTime()}.xlsx`)
    }
  }
};

</script>
<style scoped>
.tooth-diagram-container {
  margin-top: 20px;
  padding: 20px;
  border-top: 1px solid #e0e0e0;
}
</style>